import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Order } from 'src/order/entities/order.entity';
import { User } from 'src/user/entities/user.entity';
import { Repository } from 'typeorm';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { Comment } from './entities/comment.entity';

@Injectable()
export class CommentService {
  constructor(
    @InjectRepository(Comment)
    private readonly commentRepository: Repository<Comment>,
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    @InjectRepository(Order)
    private readonly orderRepository: Repository<Order>,
  ) {}
  //添加评论
  async create(info: string, openId: string, room: number, order: number) {
    let result = await this.userRepository.find({ where: { openId } });
    let nickName = result[0].nickName;
    let avatarUrl = result[0].avatarUrl;
    let comment = new Comment();
    comment.info = info;
    comment.nickName = nickName;
    comment.avatarUrl = avatarUrl;
    comment.roomId = room;
    await this.commentRepository.save(comment);
    await this.orderRepository.update(order, { status: 6 });
    return;
  }
  //查找所有评论
  findByPage(skip: number, take: number) {
    return this.commentRepository.find({ order: { id: 'DESC' }, skip, take });
  }
  //查找
  findCount() {
    return this.commentRepository.createQueryBuilder('comment').getCount();
  }
  //根据id查找
  findOne(id: number) {
    return this.commentRepository.find({ where: { roomId: id } });
  }
  //更新评论
  async update(id: number, updateCommentDto: UpdateCommentDto) {
    return await this.commentRepository.update(id, updateCommentDto);
  }
  //删除评论
  async remove(id: number) {
    return await this.commentRepository.delete(id);
  }
}
